package com.silverhaze.diamond.client;

import com.silverhaze.commons.utils.resource.ResourceAllocator;
import com.silverhaze.diamond.client.resources.BuildNumber;
import com.silverhaze.diamond.client.resources.ServiceResourceContainer;
import com.silverhaze.diamond.client.settings.SettingsStorage;
import com.silverhaze.diamond.client.settings.ui.MainFrameSettings;
import com.silverhaze.diamond.client.settings.ui.UISettings;
import com.silverhaze.diamond.client.settings.ui.customers.CustomersTabSettings;
import com.silverhaze.diamond.client.settings.ui.details.DetailsTabSettings;
import com.silverhaze.diamond.client.ui.MainFrame;
import com.silverhaze.diamond.client.ui.UIHelper;
import com.silverhaze.diamond.client.ui.dialogs.LoginDialog;
import com.silverhaze.diamond.client.ui.event.UIClientEventDispatcher;
import com.silverhaze.diamond.client.ui.frame.BasicFrame;
import com.silverhaze.diamond.client.utils.ServiceExceptionHandler;
import org.pushingpixels.substance.api.skin.SubstanceOfficeSilver2007LookAndFeel;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

/**
 * Author: Ivan Babanin © Lizard Software, 2007 y.
 */
public class Application {
    private static MainFrame frame;

    static {
        ResourceAllocator.addResourceLocationClass(ServiceResourceContainer.class);
        ResourceAllocator.addResourceLocationClass(Application.class);
    }

    public static void main(String[] args) throws IOException {
        BuildNumber.getBuildNumber();

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(new SubstanceOfficeSilver2007LookAndFeel());
                } catch (UnsupportedLookAndFeelException e) {
                    e.printStackTrace();
                }

                SettingsStorage.getSettings();
                Thread.setDefaultUncaughtExceptionHandler(new ServiceExceptionHandler());

                try {
                    Application.init();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

                UIClientEventDispatcher.getInstance();
            }
        });
    }

    public static void init() {
        ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
        toolTipManager.setInitialDelay(3000);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                LoginDialog.showDialog(new BasicFrame(), SettingsStorage
                        .getUserSettings().getCurrentUser());

                frame = new MainFrame();
                frame.setVisible(true);
            }
        });
    }

    public static void specialFinish() {
        System.exit(0);
    }

    public static void finish() {
        saveToSettings();

        UISettings uiSettings = SettingsStorage.getSettings().getUiSettings();
        uiSettings.setCustomersTabSettings(new CustomersTabSettings());
        uiSettings.setDetailsTabSettings(new DetailsTabSettings());

        SettingsStorage.saveSettings();

        specialFinish();
    }

    private static void saveToSettings() {
        Frame mainFrame = UIHelper.getMainFrame();
        MainFrameSettings settings = SettingsStorage.getSettings()
                .getUiSettings().getMainFrameSettings();

        if (mainFrame != null) {
            settings.setX((int) mainFrame.getLocation().getX());
            settings.setY((int) mainFrame.getLocation().getY());
            settings.setWidth((int) mainFrame.getSize().getWidth());
            settings.setHeight((int) mainFrame.getSize().getHeight());
        }
    }

    public static void reLogin() {
        Frame[] frames = Frame.getFrames();
        for (Frame frame : frames) {
            frame.setVisible(false);
            frame.dispose();
        }

        init();
    }

    public static MainFrame getFrame() {
        return frame;
    }
}
